package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.text.Spanned;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ShareGoldDetailBean;
import com.yiwei.zxp.api.model.ShareGoldDetailModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.DateTime;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ShareDetailActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_left_content)
    TextView txtLeftContent;
    @BindView(R.id.txt_right_content)
    TextView txtRightContent;

    private Unbinder unbiner;
    private int textSize26;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("明细详情");
        textSize26 = getResources().getDimensionPixelSize(R.dimen.x26);


        String order_id = getIntent().getStringExtra("order_id");
        getShareGoldDetail(order_id);
    }

    public void getShareGoldDetail(String order_id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ShareGoldDetailBean bean = new ShareGoldDetailBean();
        bean.order_id = order_id;
        Api.api_service.getShareGoldDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });
    }

    //    {
//        "status": "00",
//            "message": "成功",
//            "datas": {
//        "price": "100.00",   //展位单价
//                "number": 10,   //展位数据
//                "money": "998.00",   //购买金额
//                "pay_time": 1525766761,   //购买时间
//                "shared_gold": "5000.00",   //淘淘币
//                "status": 0,    // 状态：0-等待排位、1-已出彩、2-排位中
//                "true_back_time": 0,    // 已出彩时间
//                "recomm_name": "",  //推荐人姓名
//                "recomm_mobile": ""  //推荐人手机号
//    }
//    }
    private void setData(ShareGoldDetailModel.DatasBean item) {
//        状态：0-等待排位、1-已出彩、2-排位中
        String dateDesc = "", dateData = "";
        String moneyStatus = "";
        if (item.status.equals("0") || item.status.equals("2")) {
            dateDesc = "预领取时间";
            dateData = DateTime.toDate(item.last_back_time * 1000);
            txtStatus.setText("排队中");
            moneyStatus = "最多可领淘淘币";
        } else if (item.status.equals("1")) {
            dateDesc = "已领取时间";
            dateData = DateTime.toDateTime(item.true_back_time * 1000);
            txtStatus.setText("已领取");
            moneyStatus = "已领淘淘币";
        }
        Spanned txtLeft = Spans.with(this).
                font("套餐类型", textSize26, 0xff999999).
                font("\n套餐总价", textSize26, 0xff999999).
                font("\n展位数量", textSize26, 0xff999999).
                font("\n购买时间", textSize26, 0xff999999).
                font("\n" + moneyStatus, textSize26, 0xff999999).
                font("\n" + dateDesc, textSize26, 0xff999999).
                font("\n推荐人", textSize26, 0xff999999).
                font("\n推荐人手机号码", textSize26, 0xff999999).
                build();
        txtLeftContent.setText(txtLeft);

        Spanned txtRight = Spans.with(this).
                font(item.combo_name, textSize26, 0xff333333).
                font("\n"+item.money, textSize26, 0xff333333).
                font("\n" + item.number, textSize26, 0xff333333).
                font("\n" + DateTime.toDateTime(item.pay_time * 1000), textSize26, 0xff333333).
                font("\n" + item.shared_gold, textSize26, 0xff333333).
                font("\n" + dateData, textSize26, 0xff333333).
                font("\n" + item.recomm_name, textSize26, 0xff333333).
                font("\n" + item.recomm_mobile, textSize26, 0xff333333).
                build();
        txtRightContent.setText(txtRight);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
